package ua.flatmap.processor.slando;

import org.apache.commons.io.FileUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.variables.Variable;
import ua.flatmap.processor.Scrapper;
import ua.flatmap.processor.XMLReader;

import java.io.FileNotFoundException;

/**
 * Created by IntelliJ IDEA.
 * User: Kostya
 * Date: 14.07.11
 */
public class SlandoScrapper implements Scrapper {
     public void execute() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
        		"/spring/scanner-ctx.xml");

        String tempDir = FileUtils.getTempDirectoryPath() + "\\" + System.currentTimeMillis();
         ScraperConfiguration config = null;
         try {
             config = new ScraperConfiguration("flatsmap-scanner\\src\\main\\resources\\webharvest\\slando.xml");
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         }
         Scraper scraper = new Scraper(config, tempDir);

        for (int i = 1; i < 3; i++) {
            scraper.addVariableToContext("slando_url", "http://nedvizhimost.slando.com.ua/kiev/785_" + i + ".html");
            scraper.execute();
            Variable result = (Variable) scraper.getContext().get("result");
            XMLReader reader = (XMLReader)context.getBean("slandoXmlReader");
            reader.execute(result.toString());
        }

    }
}
